package com.zros.modules.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zros.common.api.vo.PageResult;
import com.zros.common.api.vo.Result;
import com.zros.common.exception.JeecgBootException;
import com.zros.modules.business.entity.Channel;
import com.zros.modules.business.entity.Shop;
import com.zros.modules.business.service.IShopService;
import com.zros.modules.business.vo.ShopVo;
import com.zros.modules.shiro.authc.util.JwtUtil;
import com.zros.modules.system.dto.UserInfo;
import com.zros.modules.system.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lvkai
 * @since 2020-04-18
 */
@RestController
@RequestMapping("/business/shop")
public class ShopController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IShopService shopService;


    /**
     * 分页查询商铺
     */
    @ApiOperation(value = "分页查询商铺")
    @RequiresPermissions("business:shop:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public PageResult<ShopVo> queryPageList(HttpServletRequest request, Page<ShopVo> page, Shop shop)
    {
        //获取当前用户
        UserInfo currentUser = new UserInfo();

        Page<ShopVo> pageShop = shopService.queryList(page, shop);
        PageResult<ShopVo> pageResult = new PageResult<ShopVo>(pageShop.getTotal(), pageShop.getRecords());
        return pageResult;

    }

    /**
     * 添加商铺
     */
    @ApiOperation(value = "添加商铺")
    @RequiresPermissions("business:shop:list")
    @PostMapping("/create")
    public Result<?> create(@RequestBody Shop shop)
    {
        QueryWrapper<Shop> ew = new QueryWrapper<>();
        ew.eq("shop_name", shop.getShopName())
                .eq("wangwang", shop.getWangwang())
                .eq("shop_type", shop.getShopType())
                .eq("shop_url", shop.getShopUrl())
                .eq("platform", shop.getPlatform());
        List<Shop> roleList = shopService.list(ew);
        if (!CollectionUtils.isEmpty(roleList)) {
            throw new JeecgBootException("该商铺已经存在！");
        }

        return new Result<>().success(shopService.save(shop) ? "添加成功" : "添加失败，请重试");
    }

    /**
     * 修改商铺
     */
    @ApiOperation(value = "修改商铺")
    @RequiresPermissions("business:shop:list")
    @PostMapping("/update")
    public Result<?> update(@RequestBody Shop shop)
    {
        QueryWrapper<Shop> ew = new QueryWrapper<>();
        ew.ne("shop_id", shop.getShopId())
                .eq("shop_name", shop.getShopName())
                .eq("wangwang", shop.getWangwang())
                .eq("shop_type", shop.getShopType())
                .eq("shop_url", shop.getShopUrl())
                .eq("platform", shop.getPlatform());
        List<Shop> roleList = shopService.list(ew);
        if (!CollectionUtils.isEmpty(roleList)) {
            throw new JeecgBootException("该商铺已经存在！");
        }

        return new Result<>().success(shopService.updateById(shop) ? "修改成功" : "修改失败，请重试");
    }

    /**
     *  获取当前用户
     */
    UserInfo queryCurrentUser(HttpServletRequest request,String queryMethod)
    {
        UserInfo currentUser = new UserInfo();
        String userAccount = JwtUtil.getUserAccountByToken(request);
        currentUser = userService.queryUserInfoByAccount(userAccount,queryMethod);

        return currentUser;
    }



}
